################################## 
############ Preamble ############ 
################################## 

# set language to English
Sys.setenv(LANG = "en")

# clean up
rm(list = ls())

# Set working directory: please set your own
#setwd("~/Dropbox/JOP third submission/JOP replication/")

# load necessary packages
library(tidyverse)
library(haven)

######################################### 
############ End of preamble ############ 
######################################### 

# Import the data
reported_vote_data <- read_dta("01_data/reported_vote_all.dta")

# Make the plot
reported_vote_plot <- reported_vote_data %>%
  mutate(Date = as.Date(election_date)) %>%
  ggplot(aes(x = Date, y = reported_vote)) +
  geom_point(size = 3, aes(shape = Victory, color = Victory)) +
  geom_line(color = "black", linetype = "dashed") +
  scale_color_manual(values = c("red", "blue")) +
  scale_y_continuous(limits = c(0, 1.15), breaks = c(0, 0.25, 0.5, 0.75, 1)) +
  theme_bw() +
  labs(x = "Election Date", y = "Proportion of the official vote for PP that is reported in post-electoral surveys") +
  geom_hline(aes(yintercept = 1.054964)) +
  annotate(geom = "text", x = as.Date("2010-01-01", "%Y-%m-%d"), y = 1.08, 
           label = "Mean for parties winning elections in CSES", 
           color = "black",
           size = 3) 
reported_vote_plot

ggsave("04_plots/fig1.png", plot = reported_vote_plot, width = 17, height = 15, units = "cm")